package com.rc.saas.mini.service.invoice.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rc.saas.mini.mapper.invoice.InvoiceAddressMapper;
import com.rc.saas.mini.model.invoice.InvoiceAddress;
import com.rc.saas.mini.service.invoice.InvoiceAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author wangzhengwei
 * @create 2021/5/12
 */
@Service
public class InvoiceAddressServiceImpl implements InvoiceAddressService {
    @Autowired
    private InvoiceAddressMapper invoiceAddressMapper;

    @Override
    public InvoiceAddressMapper getMapper() {
        return invoiceAddressMapper;
    }

    @Override
    public PageInfo<InvoiceAddress> listInvoiceAddressByMemberId(Long memberId, Integer pageNo, Integer pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        List<InvoiceAddress> list = invoiceAddressMapper.listInvoiceAddressByMemberId(memberId);
        return new PageInfo<>(list);
    }

    @Override
    public List<InvoiceAddress> listByCondition(InvoiceAddress invoiceAddress) {
        return invoiceAddressMapper.listByCondition(invoiceAddress);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdateInvoiceAddress(InvoiceAddress invoiceAddress) {

        if ("1".equals(invoiceAddress.getIsDefault())) {
            invoiceAddressMapper.updateNotDefault(invoiceAddress.getMemberId());
        }

        if (invoiceAddress.getInvoiceAddressId() == null) {
            invoiceAddress.setCreatedTime(new Date());
            invoiceAddressMapper.insertSelective(invoiceAddress);
        } else {
            invoiceAddress.setLastUpdatedTime(new Date());
            invoiceAddressMapper.updateByPrimaryKeySelective(invoiceAddress);
        }
    }
}
